package com.ycy.juel;

import com.ycy.BaseTest;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.Test;

import java.util.*;

public class SkipExpression extends BaseTest {

  @Test
  public void creatProcDef() {
    // 部署流程
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment()
            .addClasspathResource("juel/h.bpmn20.xml")
            .deploy();
    System.out.println(deployment);
  }

//  @Test
//  public void createProcInst() {
//    RuntimeService runtimeService = processEngine.getRuntimeService();
//    HashMap<String, Object> variables = new HashMap<>();
//    List<String> zhixingrens = new ArrayList<>();
//    zhixingrens.add("ycy");
//    zhixingrens.add("xdm");
//    variables.put("ASSIGNEE_zhixingren_LIST", zhixingrens);
//
//    variables.put(ACTIVITY_SKIP_ENABLE_KEY, true);
//
//    ProcessInstance processInstance =
//            runtimeService.startProcessInstanceByKey("skiping", variables);
//    System.out.println(processInstance.getProcessInstanceId());
//  }

  public static final String ACTIVITY_SKIP_ENABLE_KEY = "_FLOWABLE_SKIP_EXPRESSION_ENABLED";

  /**
   * 测试bpmn中的，activiti:skipExpression
   */
  @Test
  public void createProcInst2() {

    RuntimeService runtimeService = processEngine.getRuntimeService();

    HashMap<String, Object> variables = new HashMap<>();

    List<Long> B = new LinkedList<>();
    B.add(1356172011664384002L);
    variables.put("RDFA_passed_assignee_List", B);

//    List<Long> A = new LinkedList<>();
//    A.add(1356172011664384002L);
//    variables.put("ASSIGNEE_B_LIST", A);

    variables.put(ACTIVITY_SKIP_ENABLE_KEY, true);

    List<String> zhixingrenss = new ArrayList<>();
    zhixingrenss.add("ycyC");
    zhixingrenss.add("xdmC");
    variables.put("ASSIGNEE_B_LIST", zhixingrenss);

    ProcessInstance processInstance =
            runtimeService.startProcessInstanceByKey("skiping", variables);
    String processInstanceId = processInstance.getProcessInstanceId();
    System.out.println(processInstanceId);

  }

  /**
   * 查询流程实例下的所有任务
   */
  @Test
  public void queryProcessInstanceIdTask() {
    TaskService taskService = processEngine.getTaskService();
    //获取流程实例中的task
    List<Task> list = taskService.createTaskQuery().processInstanceId("25001").list();
    for (Task task : list) {
      System.out.println(task);
      //查询所有参数
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  /**
   * 查询流程实例下的所有任务
   */
  @Test
  public void queryProcessInstanceIdHisTask() {
    TaskService taskService = processEngine.getTaskService();
    HistoryService historyService = processEngine.getHistoryService();
    List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId("60001").list();
    for (HistoricTaskInstance historicTaskInstance : list) {
      System.out.println(historicTaskInstance.getId() + " " + historicTaskInstance.getTaskDefinitionKey() + " " + historicTaskInstance.getEndTime() + " " + historicTaskInstance.getAssignee());
    }
  }


}
